using Microsoft.Extensions.DependencyInjection;
using Models;
using Services;
using System;
using System.Collections.Generic;

namespace Examples
{
    /// <summary>
    /// 动态年份查询使用示例
    /// </summary>
    public class DynamicYearQueryExample
    {
        private readonly IServiceProvider _serviceProvider;
        private readonly WorkPieceDataCaptureRespository _repository;

        public DynamicYearQueryExample(IServiceProvider serviceProvider)
        {
            _serviceProvider = serviceProvider;
            _repository = new WorkPieceDataCaptureRespository(serviceProvider);
        }

        /// <summary>
        /// 示例：查询指定年份的数据
        /// </summary>
        public void QueryDataByYear()
        {
            // 创建查询条件
            var conditions = new model_SearchCondition
            {
                dDate_Begin = new DateTime(2025, 1, 1),
                dDate_End = new DateTime(2025, 12, 31),
                ZC_Model = "某型号",
                ZC_Result = "OK"
            };

            // 查询2025年的数据
            var result = _repository.SearchDataFromHistoryTableByYear(2025, conditions);
            
            if (result.bSuccess)
            {
                Console.WriteLine($"查询成功，共找到 {result.obj_Return.Count} 条记录");
                foreach (var item in result.obj_Return)
                {
                    Console.WriteLine($"数据：{item}");
                }
            }
            else
            {
                Console.WriteLine($"查询失败：{result.strMsg}");
            }
        }

        /// <summary>
        /// 示例：查询跨年份的数据
        /// </summary>
        public void QueryDataAcrossYears()
        {
            var conditions = new model_SearchCondition
            {
                dDate_Begin = new DateTime(2024, 12, 1),
                dDate_End = new DateTime(2025, 2, 28),
                ZC_Result = "OK"
            };

            var allResults = new List<object>();

            // 动态查询跨年份数据
            for (var year = conditions.dDate_Begin.Value.Year; year <= conditions.dDate_End.Value.Year; year++)
            {
                var yearResult = _repository.SearchDataFromHistoryTableByYear(year, conditions);
                if (yearResult.bSuccess)
                {
                    allResults.AddRange(yearResult.obj_Return);
                    Console.WriteLine($"{year}年查询成功，找到 {yearResult.obj_Return.Count} 条记录");
                }
                else
                {
                    Console.WriteLine($"{year}年查询失败：{yearResult.strMsg}");
                }
            }

            Console.WriteLine($"跨年份查询完成，总共找到 {allResults.Count} 条记录");
        }

        /// <summary>
        /// 示例：测试新年份表的自动支持
        /// </summary>
        public void TestNewYearTableSupport()
        {
            // 假设数据库中新建了2031年的表：tb_WorkPieceDataCapture_History_2031
            // 并且在AppDbContext中添加了对应的DbSet属性
            
            var conditions = new model_SearchCondition
            {
                dDate_Begin = new DateTime(2031, 1, 1),
                dDate_End = new DateTime(2031, 12, 31)
            };

            // 无需修改任何代码，直接查询新年份
            var result = _repository.SearchDataFromHistoryTableByYear(2031, conditions);
            
            if (result.bSuccess)
            {
                Console.WriteLine("2031年表查询成功！新表自动支持正常工作。");
            }
            else
            {
                Console.WriteLine($"2031年表查询失败：{result.strMsg}");
                // 如果失败，可能是因为：
                // 1. 数据库中没有创建对应的表
                // 2. AppDbContext中没有添加对应的DbSet属性
                // 3. 没有创建对应的实体类
            }
        }

        /// <summary>
        /// 示例：复杂查询条件
        /// </summary>
        public void QueryWithComplexConditions()
        {
            var conditions = new model_SearchCondition
            {
                dDate_Begin = new DateTime(2025, 1, 1),
                dDate_End = new DateTime(2025, 12, 31),
                ZC_SerialNo = "ABC",
                ZC_Model = "型号A",
                ZC_Result = "OK",
                FLJ_YCQ_Result = "OK",
                JCY_QM_Pressure_Begin = 100.0,
                JCY_QM_Pressure_End = 200.0,
                JCY_QM_Leak_Begin = 0.1,
                JCY_QM_Leak_End = 1.0
            };

            var result = _repository.SearchDataFromHistoryTableByYear(2025, conditions);
            
            if (result.bSuccess)
            {
                Console.WriteLine($"复杂条件查询成功，找到 {result.obj_Return.Count} 条符合条件的记录");
            }
            else
            {
                Console.WriteLine($"复杂条件查询失败：{result.strMsg}");
            }
        }
    }

    /// <summary>
    /// 新年份表支持说明
    /// </summary>
    public static class NewYearTableSupportGuide
    {
        /// <summary>
        /// 添加新年份表的步骤说明
        /// </summary>
        public static void PrintInstructions()
        {
            Console.WriteLine("=== 添加新年份表支持的步骤 ===");
            Console.WriteLine("1. 在数据库中创建新的年份表，例如：tb_WorkPieceDataCapture_History_2031");
            Console.WriteLine("2. 创建对应的实体类，例如：tb_WorkPieceDataCapture_History_2031.cs");
            Console.WriteLine("3. 在AppDbContext.cs中添加对应的DbSet属性：");
            Console.WriteLine("   public DbSet<tb_WorkPieceDataCapture_History_2031> tb_workpiecedatacapture_history_2031 { get; set; }");
            Console.WriteLine("4. 在OnModelCreating方法中添加表映射配置：");
            Console.WriteLine("   modelBuilder.Entity<tb_WorkPieceDataCapture_History_2031>().ToTable(\"tb_WorkPieceDataCapture_History_2031\").HasKey(e => e.iID);");
            Console.WriteLine("5. 完成！无需修改任何查询代码，新年份表将自动支持查询。");
            Console.WriteLine("");
            Console.WriteLine("=== 优势 ===");
            Console.WriteLine("✓ 零代码修改：添加新年份表后无需修改任何查询逻辑");
            Console.WriteLine("✓ 自动发现：系统会自动发现并使用新的年份表");
            Console.WriteLine("✓ 类型安全：使用反射和表达式树确保类型安全");
            Console.WriteLine("✓ 性能优化：动态构建的查询会被EF Core优化");
            Console.WriteLine("✓ 错误处理：提供详细的错误信息，便于调试");
        }
    }
}
